package com.jsbb.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Redis工具类
 *
 * @author w
 * @date 2021-03-05
 */
@Slf4j
@Component
public class RedisUtils implements ApplicationContextAware {

    private static RedisTemplate<Object, Object> redisTemplate;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        RedisUtils.redisTemplate = (RedisTemplate<Object, Object>) applicationContext.getBean("redisTemplate");
        log.info("注入RedisTemplate");
    }

    /**
     * 删除指定缓存
     *
     * @param key key
     */
    public static void delete(String... key) {
        if (key != null && key.length > 0) {
            if (key.length == 1) {
                redisTemplate.delete(key[0]);
            } else {
                redisTemplate.delete(CollectionUtils.arrayToList(key));
            }
        }
    }

    /**
     * 设置指定缓存失效时间
     *
     * @param key  key
     * @param time 秒
     * @return
     */
    public static boolean expire(String key, long time) {
        try {
            if (time > 0) {
                redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }
            return true;
        } catch (Exception e) {
            log.error("设置指定缓存失效时间异常==={}", e);
            return false;
        }
    }

    public static boolean set(String key, Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            log.error("String.set异常==={}", e);
            return false;
        }
    }

    public static boolean set(String key, Object value, long time) {
        try {
            if (time > 0) {
                redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
            } else {
                set(key, value);
            }
            return true;
        } catch (Exception e) {
            log.error("String.set异常==={}", e);
            return false;
        }
    }

    public static Object get(String key) {
        return key == null ? null : redisTemplate.opsForValue().get(key);
    }


    public static boolean put(String key, String item, Object value) {
        try {
            redisTemplate.opsForHash().put(key, item, value);
            return true;
        } catch (Exception e) {
            log.error("Map.put异常==={}", e);
            return false;
        }
    }

    public static boolean put(String key, String item, Object value, long time) {
        try {
            redisTemplate.opsForHash().put(key, item, value);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            log.error("Map.put异常==={}", e);
            return false;
        }
    }

    public static Object get(String key, String item) {
        return redisTemplate.opsForHash().get(key, item);
    }

    public static Map<Object, Object> entries(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    public static void delete(String key, Object... item) {
        redisTemplate.opsForHash().delete(key, item);
    }


    public static boolean rightPush(String key, Object value) {
        try {
            redisTemplate.opsForList().rightPush(key, value);
            return true;
        } catch (Exception e) {
            log.error("List.rightPush异常==={}", e);
            return false;
        }
    }

    public static boolean rightPush(String key, Object value, long time) {
        try {
            redisTemplate.opsForList().rightPush(key, value);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            log.error("List.rightPush异常==={}", e);
            return false;
        }
    }

    public static List<Object> range(String key, long start, long end) {
        try {
            return redisTemplate.opsForList().range(key, start, end);
        } catch (Exception e) {
            log.error("List.range异常==={}", e);
            return null;
        }
    }
}
